package com.example.service;

import com.example.mapper.TagMapper;
import com.example.utils.TimeUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import com.example.entity.Tag;
import javax.annotation.Resource;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class TagService {
    @Resource
    private TagMapper tagMapper;
    public long save(Tag tag){
        tag.setDelTag("0");
        tag.setCreateTime(Timestamp.valueOf(LocalDateTime.now()));
        return tagMapper.insert(tag);
    }

    public long deleteById(Integer id){
        return tagMapper.deleteById(id);
    }
    public long deleteBatch(List<Integer> ids) {
        long sum = 0;
        for (Integer id : ids) {
            sum += tagMapper.deleteById(id);
        }
        return sum;
    }

    public long updateById(Tag tag){
        return tagMapper.updateById(tag);
    }
    public List<Tag> selectAll(Tag tag){
        return tagMapper.selectAll(tag);
    }
    public PageInfo<Tag> selectPage(Tag tag,Integer pageNum,Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<Tag> list = tagMapper.selectAll(tag);
        list.forEach(entity->entity.setReleaseTime(TimeUtils.TimestampToDateFormat(entity.getCreateTime())));
        return PageInfo.of(list);
    }
}
